package com.zzl.java8.czbk.lambda.sort.demo2;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * description: 数组排序小样儿2
 * @author 朱正磊
 * @date 2022-06-27 10:45:13
 */
public class ArraySortedDemo2 {

    /**
     * description: 测试按需排序（升序）（使用常规接口实现类）
     * @author 朱正磊
     * @date 2022-06-27 10:51:52
     */
    @Test
    void test1() {
        int[] intArray = {3, 6, 9, 1, 4, 8, 7, 0, 5};
        this.sorted(intArray, new SortedCompareImpl1());
        Stream<int[]> stream = Stream.of(intArray);
        Stream<Integer> integerStream = stream.flatMap(x -> Arrays.stream(x).boxed());
        String result = integerStream.map(String::valueOf).collect(Collectors.joining(", "));
        // result = 0, 1, 3, 4, 5, 6, 7, 8, 9
        System.out.println("result = " + result);
    }

    /**
     * description: 测试按需排序（降序）（使用常规接口实现类）
     * @author 朱正磊
     * @date 2022-06-27 10:57:09
     */
    @Test
    void test2() {
        int[] intArray = {3, 6, 9, 1, 4, 8, 7, 0, 5};
        this.sorted(intArray, new SortedCompareImpl2());
        Stream<int[]> stream = Stream.of(intArray);
        Stream<Integer> integerStream = stream.flatMap(x -> Arrays.stream(x).boxed());
        String result = integerStream.map(String::valueOf).collect(Collectors.joining(", "));
        // result = 9, 8, 7, 6, 5, 4, 3, 1, 0
        System.out.println("result = " + result);
    }

    /**
     * description: 测试按需排序（升序）（使用匿名内部类）
     * @author 朱正磊
     * @date 2022-06-27 10:57:54
     */
    @Test
    void test3() {
        int[] intArray = {3, 6, 9, 1, 4, 8, 7, 0, 5};
        this.sorted(intArray, new SortedCompare() {
            @Override
            public boolean compare(int a, int b) {
                return a > b;
            }
        });
        Stream<int[]> stream = Stream.of(intArray);
        Stream<Integer> integerStream = stream.flatMap(x -> Arrays.stream(x).boxed());
        String result = integerStream.map(String::valueOf).collect(Collectors.joining(", "));
        // result = 0, 1, 3, 4, 5, 6, 7, 8, 9
        System.out.println("result = " + result);
    }

    /**
     * description: 测试按需排序（降序）（使用匿名内部类）
     * @author 朱正磊
     * @date 2022-06-27 10:59:22
     */
    @Test
    void test4() {
        int[] intArray = {3, 6, 9, 1, 4, 8, 7, 0, 5};
        this.sorted(intArray, new SortedCompare() {
            @Override
            public boolean compare(int a, int b) {
                return a < b;
            }
        });
        Stream<int[]> stream = Stream.of(intArray);
        Stream<Integer> integerStream = stream.flatMap(x -> Arrays.stream(x).boxed());
        String result = integerStream.map(String::valueOf).collect(Collectors.joining(", "));
        // result = 9, 8, 7, 6, 5, 4, 3, 1, 0
        System.out.println("result = " + result);
    }

    /**
     * description: 测试按需排序（升序）（使用lambda表达式，函数式编程）
     * @author 朱正磊
     * @date 2022-06-27 11:00:12
     */
    @Test
    void test5() {
        int[] intArray = {3, 6, 9, 1, 4, 8, 7, 0, 5};
        this.sorted(intArray, (a, b) -> a > b);
        Stream<int[]> stream = Stream.of(intArray);
        Stream<Integer> integerStream = stream.flatMap(x -> Arrays.stream(x).boxed());
        String result = integerStream.map(String::valueOf).collect(Collectors.joining(", "));
        // result = 0, 1, 3, 4, 5, 6, 7, 8, 9
        System.out.println("result = " + result);
    }

    /**
     * description: 测试按需排序（降序）（使用lambda表达式，函数式编程）
     * @author 朱正磊
     * @date 2022-06-27 11:00:18
     */
    @Test
    void test6() {
        int[] intArray = {3, 6, 9, 1, 4, 8, 7, 0, 5};
        this.sorted(intArray, (a, b) -> a < b);
        Stream<int[]> stream = Stream.of(intArray);
        Stream<Integer> integerStream = stream.flatMap(x -> Arrays.stream(x).boxed());
        String result = integerStream.map(String::valueOf).collect(Collectors.joining(", "));
        // result = 9, 8, 7, 6, 5, 4, 3, 1, 0
        System.out.println("result = " + result);
    }

    /**
     * description: 按需排序
     * @author 朱正磊
     * @date 2022-06-27 10:50:50
     * @param intArray 整形数组
     * @param sortedCompare 排序接口
     */
    private void sorted(int[] intArray, SortedCompare sortedCompare) {
        for (int i = 0; i < intArray.length; i++) {
            for (int j = i + 1; j < intArray.length; j++) {
                if (sortedCompare.compare(intArray[i], intArray[j])) {
                    int temp = intArray[i];
                    intArray[i] = intArray[j];
                    intArray[j] = temp;
                }
            }
        }
    }

}
